﻿using System;
using Castle.Core.Configuration;
using Castle.MicroKernel;

namespace Mbs.CastleContainer
{
    /// <summary>
    /// Mbs 装备基类
    /// </summary>
    /// <remarks>
    /// 采用类适配器的方式实现Mbs装备向Kernel监听器进行适配的目的
    /// </remarks>
    [Serializable]
    public class MbsFacility:KernelListnerAdapter, IFacility,IDisposable
    {
        protected IMbsKernel Kernel { get; private set; }
        protected IConfiguration FacilityConfig { get; private set; }

        protected MbsFacility(KernelListnerType type) : base(type) { }

        protected virtual void Init(){}

        public void Init(IKernel kernel, IConfiguration facilityConfig)
        {
            this.Kernel = kernel as IMbsKernel;
            this.FacilityConfig = facilityConfig;

            if (Kernel != null)
                Kernel.ListnerManager.Register(this);

            Init();
        }

        
        public void Terminate()
        {
            if (Kernel != null)
                Kernel.ListnerManager.Register(this);
            Dispose();
            Kernel = null;
        }

        public virtual void Dispose() { }
    }
}
